﻿using NovoSerDes.A2B.Forms;
using ST.Library.UI.NodeEditor;
using STNode_A2B.STNodes;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NovoSerDes.A2B.UIPages
{
    public partial class Configuration : UIPage
    {
        public Configuration()
        {
            InitializeComponent();        
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            stNodeEditorPannel1.LoadAssembly(Application.ExecutablePath);
            //stNodeEditorPannel1.Editor.Nodes.Add(new STNode_Generic());
            stNodeEditorPannel1.Editor.BackColor = Color.White;
            stNodeEditorPannel1.Editor.ShowGrid = false;
            //stNodeEditorPannel1.Editor.ActiveChanged += (s, e) => stNodeEditorPannel1.PropertyGrid.SetNode(stNodeEditorPannel1.Editor.ActiveNode);
            stNodeEditorPannel1.Editor.NodeAdded += (s, ea) =>
            {
                ea.Node.ContextMenuStrip = contextMenuStrip1;
                contextMenuStrip1.Tag = ea.Node;
            };
            
        }
        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (stNodeEditorPannel1.Editor.ActiveNode == null) return;
            stNodeEditorPannel1.Editor.Nodes.Remove(stNodeEditorPannel1.Editor.ActiveNode);

        }

        private void lockLocationToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (stNodeEditorPannel1.Editor.ActiveNode == null) return;
            stNodeEditorPannel1.Editor.ActiveNode.LockLocation = !stNodeEditorPannel1.Editor.ActiveNode.LockLocation;
        }

        private void lockConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stNodeEditorPannel1.Editor.ActiveNode.LockOption = !stNodeEditorPannel1.Editor.ActiveNode.LockOption;
        }

        private void proToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            if (menuItem != null)
            {
                // 获取 contextMenuStrip1
                ContextMenuStrip contextMenuStrip = menuItem.Owner as ContextMenuStrip;
                if (contextMenuStrip != null)
                {
                    // 通过 Tag 属性获取 STNode 实例
                    STNode stNode = contextMenuStrip.Tag as STNode;
                    if (stNode != null)
                    {
                        if (stNode is STNode_Master_Node)
                        {
                            Master_propetites master_Propetites = new Master_propetites();
                            master_Propetites.Show();
                        }
                        else
                        {
                            Propeties propeties = new Propeties();
                            propeties.ShowDialog();
                        }
                    }
                    else
                    {
                        MessageBox.Show("The ContextMenuStrip's Tag is not of type STNode.");
                    }
                }
            }

            //STNode node = (STNode)sender;
            //if(node == null) return;
            //else if(node is STNode_Master_Node)
            //{
            //    Master_propetites master_Propetites =  new Master_propetites();
            //    master_Propetites.Show();
            //}
            //else
            //{
            //    Propeties propeties = new Propeties();
            //    propeties.ShowDialog();
            //}

        }

        private void checkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
            if (menuItem != null)
            {
                // 获取 contextMenuStrip1
                ContextMenuStrip contextMenuStrip = menuItem.Owner as ContextMenuStrip;
                if (contextMenuStrip != null)
                {
                    // 通过 Tag 属性获取 STNode 实例
                    STNode stNode = contextMenuStrip.Tag as STNode;
                    if (stNode != null)
                    {
                        if(stNode is STNode_Host)
                        {
                            STNode_Host node = stNode as STNode_Host;
                            bool isAllConnect = node.CheckInputOptionsIsConnect();
                            MessageBox.Show(isAllConnect.ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("The ContextMenuStrip's Tag is not of type STNode.");
                    }
                }
            }
        }
    }
}
